package org.delphy.indy2.containers
{
	import caurina.transitions.Tweener;
	
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.BlurFilter;
	import flash.geom.Matrix;
	import flash.geom.Point;
	
	import mx.containers.Canvas;
	import mx.core.UIComponent;
	import mx.managers.PopUpManager;

	public class CarbonDropdownWindow extends Canvas
	{

		private var hostMask:Sprite;
		private var hostMaskUI:UIComponent;
		
		private var _host:DisplayObject=null;
		
		public function get host():DisplayObject
		{ 
			return _host; 
		}

		public function set host(value:DisplayObject):void
		{
			if (value !== _host)
			{
				_host = value;
			}
		}
		
		public var transitionTime:Number=1.3;
		
		private var shadowSize:Number;
		private var shadowDistance:Number;
		
		public var enableBackgroundBlur:Boolean=true;
		
		public function CarbonDropdownWindow(parentObject:DisplayObject=null)
		{
			super();
			
			styleName="carbonDropdownWindow";
			
			visible=false;
			
			if(parentObject!=null)
				_host=parentObject;
				
		}
		
		public function popUp():void{		
			
			if(Tweener.isTweening(this)){
				return;
			}
			
			if(_host==null)
				_host=parent;

			shadowSize=getStyle("shadowSize")==undefined?16:getStyle("shadowSize");
			shadowDistance=getStyle("shadowDistance")==undefined?3:getStyle("shadowDistance");
			
			if(enableBackgroundBlur)
				drawHostMask();
			
			// Initialize position
			var hostCenter:Point=new Point(_host.x,_host.y);
			hostCenter=_host.parent.localToGlobal(hostCenter);
			
			x=hostCenter.x+ (_host.width-this.width)/2;
			y=hostCenter.y-this.height;

			// Create a mask
			var aMask:Sprite=new Sprite();
			var g:Graphics=aMask.graphics;
			g.beginFill(0x000000,1);
			g.drawRect(x,hostCenter.y,width+(shadowSize+shadowDistance)*2,height+(shadowDistance+shadowSize)*2);
			g.endFill();
			
			this.mask=aMask;
			
			PopUpManager.addPopUp(this,_host,false);			
			
			if(!visible){
				visible=true;
			}
			
			Tweener.addTween(this,{y:hostCenter.y,time:transitionTime});
		}
		
		public function close():void{
			var hostCenter:Point=new Point(_host.x,_host.y);
			hostCenter=_host.parent.localToGlobal(hostCenter);
			
			var posY:Number=hostCenter.y-this.height;
			Tweener.addTween(this,{y:posY,time:transitionTime,onComplete:onCloseAnimationComplete});
		}
		
		private function drawHostMask():void{
			var hostCenter:Point=new Point(_host.x,_host.y);
			hostCenter=_host.parent.localToGlobal(hostCenter);

			hostMask=new Sprite();
			
			var hostBitmap:BitmapData=new BitmapData(_host.width,_host.height,true);
			hostBitmap.draw(_host);

			var g:Graphics=hostMask.graphics;
			var mat:Matrix=new Matrix();
			mat.translate(hostCenter.x,hostCenter.y);
			g.beginBitmapFill(hostBitmap,mat);
			g.drawRect(hostCenter.x,hostCenter.y,_host.width,_host.height);
			g.endFill();
			
			g.beginFill(0x000000,0.5);
			g.drawRect(hostCenter.x,hostCenter.y,_host.width,_host.height);
			g.endFill();			
			
			hostMask.addEventListener(MouseEvent.CLICK,dummy);
			hostMask.addEventListener(MouseEvent.DOUBLE_CLICK,dummy);
			hostMask.addEventListener(MouseEvent.ROLL_OVER,dummy);
			hostMask.addEventListener(MouseEvent.ROLL_OUT,dummy);
			
			hostMaskUI=new UIComponent();
			hostMaskUI.addChild(hostMask);
			
			hostMaskUI.filters=[new BlurFilter(6,6)];
			
			PopUpManager.addPopUp(hostMaskUI,_host,false);	
		}
		
		private function onCloseAnimationComplete():void{
			if(enableBackgroundBlur)
				PopUpManager.removePopUp(hostMaskUI);
			PopUpManager.removePopUp(this);
		}
		
		private function dummy(evt:MouseEvent):void{
		}
		
	}
}